import 'package:cashbook/custom/NetLoadingDialog.dart';
import 'package:cashbook/model/user_vo.dart';
import 'package:cashbook/utils/dialogutil.dart';
import 'package:cashbook/utils/loadingdialog.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/utils/toast.dart';
import 'package:cashbook/utils/uuid_util.dart';
import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';
import 'package:uuid/uuid_util.dart';

class RegisterPage extends StatefulWidget {
  String title;

  RegisterPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RegisterState();
  }
}

class RegisterState extends State<RegisterPage> {
  var nameC = TextEditingController();
  var psdC = TextEditingController();
  var nickC = TextEditingController();
  var spsdC = TextEditingController();
  var answer1 = TextEditingController();
  var answer2 = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      resizeToAvoidBottomPadding: true, //输入框抵住键盘
      appBar: AppBar(
        title: Text(this.widget.title),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.all(5),
          child: Column(
            children: <Widget>[
              Text(
                '基本信息',
                style: TextStyle(color: Colors.grey, fontSize: 12),
              ),
              Card(
                child: Container(
                  margin: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      TextField(
                        controller: nameC,
                        decoration: setInputDecoration('账户名称'),
                      ),
                      TextField(
                        controller: psdC,
                        decoration: setInputDecoration('密码'),
                      ),
                      TextField(
                        controller: spsdC,
                        decoration: setInputDecoration('确认密码'),
                      ),
                      TextField(
                        controller: nickC,
                        decoration: setInputDecoration('昵称'),
                      ),
                    ],
                  ),
                ),
              ),
              Text(
                '找回账号信息',
                style: TextStyle(color: Colors.grey, fontSize: 12),
              ),
              Card(
                child: Container(
                  margin: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 40,
                        alignment: Alignment.centerLeft,
                        child: Text(Manager.instance.ask1),
                      ),
                      TextField(
                        controller: answer1,
                        decoration: setInputDecoration('答案'),
                      ),
                      Container(
                        height: 40,
                        alignment: Alignment.centerLeft,
                        child: Text(Manager.instance.ask1),
                      ),
                      TextField(
                        controller: answer2,
                        decoration: setInputDecoration('答案'),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 20),
                child: RaisedButton(
                  color: Colors.blue,
                  onPressed: () {
                    registerAction();
                  },
                  child: Text('注册'),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  setInputDecoration(String hintText) {
    return InputDecoration(hintText: hintText);
  }

  void registerAction() async {
    var loading = LoadingDialog(text: '加载中...');

    if (nameC == null || nameC.text.length <= 0) {
      print('用户名不能为空');
      Toast.toast(context, '用户名不能为空');
      return;
    }
    if (psdC.text.length <= 0) {
      Toast.toast(context, '密码不能为空');
      return;
    }
    if (spsdC.text.length <= 0) {
      Toast.toast(context, '确认密码不能为空');
      return;
    }
    if (spsdC.text != psdC.text) {
      Toast.toast(context, '2次密码不一致');
      return;
    }
    User u = User();
    u.userUuid = UUID.getUuid();
    u.name = nameC.text;
    u.psd = psdC.text;
    u.nickName = nickC.text;
    u.answer1 = answer1.text;
    u.answer2 = answer2.text;

    User res = await UserProvider().insert(u);
    if (res != null && res.autoId > 0) {
//      if(null!=loading){
//        loading.dismiss();
//      }
      DialogUtil.showAlertDialog(context, '提示', '添加用户成功！');
    }

    nameC.text = '';
    psdC.text = '';
    spsdC.text = '';
    nickC.text = '';
    answer1.text = '';
    answer2.text = '';
  }

  _register() {}
}
